package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BookRespository;
import myBeans.BookInfo;
import myBeans.UserInfo;

/**
 * Servlet implementation class UpdateBookReviewServlet
 */
@WebServlet("/UpdateBookReview.do")
public class UpdateBookReviewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateBookReviewServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		Connection conn = (Connection) getServletContext().getAttribute("connection");

		String bookISBN = request.getParameter("isbn");
		String reviewTitle = request.getParameter("reviewTitle");		
		String reviewDetail = request.getParameter("reviewDetail");
		String reviewVideoPathFile = request.getParameter("reviewVideoPathFile");
		String reviewId = request.getParameter("reviewId");
		
		//UserInfo user = (UserInfo)request.getSession().getAttribute("User");
		String userId = ""; //user.getEmail();
		
		String sql = "";
		PreparedStatement preStm;
		
		try {
			if(reviewId.isEmpty()){
				sql="Insert Into TBInformation(title, detail, Info_Type, createby, createdate, PathFile, ISBN)" +
						" Values(?,?,7,?,now(),?,?)";
				
				preStm = conn.prepareStatement(sql);
				
				preStm.setString(1, reviewTitle);	
				preStm.setString(2, reviewDetail);	
				preStm.setString(3, userId);	
				preStm.setString(4, reviewVideoPathFile);	
				preStm.setString(5, bookISBN);	
				
			}
			else
			{
				sql="Update TBInformation set title=?,detail=?,PathFile=? Where id=?;";
				
				preStm = conn.prepareStatement(sql);
				
				preStm.setString(1, reviewTitle);
				preStm.setString(2, reviewDetail);
				preStm.setString(3, reviewVideoPathFile);
				preStm.setInt(4, Integer.parseInt(reviewId));
			}
			
			int rowEffect = preStm.executeUpdate();
			
			if(rowEffect > 0){
				response.sendRedirect("AdminReviewBookList.jsp");
			}
			else
			{
				PrintWriter out = response.getWriter();
				response.setContentType("text/html");
				out.println("<h1>Update/Insert Error, please try again.</h1>");
				out.close();
			}
			
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
